<template>
	<view class="page">
		<!-- 优惠券tab -->
		<view class="coupon-tab">
			<view class="tab" :class="{'action':TabShow===1}" @click="onCouponTab(1)">
				<text>未使用</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':TabShow===2}" @click="onCouponTab(2)">
				<text>已使用</text>
				<text class="line"></text>
			</view>
			<view class="tab" :class="{'action':TabShow===3}" @click="onCouponTab(3)">
				<text>已过期</text>
				<text class="line"></text>
			</view>
		</view>
		<!-- 优惠券列表 -->
		<view class="coupon-list" v-if="goodsList.length!=0">
			<view class="list" v-for="(item,index) in goodsList" :key="index">
				<view class="list-data" :class="{'coupon-lose':TabShow!=1}">
					<view class="coupon-price" v-if="item.price!=0">
						<view class="discounts">
							<text class="min">￥</text>
							<text class="max">{{item.price}}</text>
						</view>
						<view class="full-reduction"><text>满{{item.full_price}}元减{{item.price}}元</text></view>
						<view class="jag"></view>
					</view>
					<view class="coupon-price" v-if="item.price==0">
						<view class="discounts">
							<text class="max">1张</text>
						</view>
						
					</view>
					<view class="coupon-info">
						<view class="info-title">
							<!-- <view class="tag"><text>限品类券</text></view> -->
							<view class="title"><text>{{item.name}}</text></view>
						</view>
						<!-- <view class="date-get">
							<view class="date"><text>开始时间:{{item.created_at}}</text></view>
						</view> -->
						<view class="date-get">
							<view class="date"><text>{{item.created_at||'2020.10.10'}}-{{item.end_time}}</text></view>
							<view class="get" @click="onCouponUse(item)" v-if="TabShow===1">
								<text>立即使用</text>
							</view>
						</view>
						<view class="describe-title" @click="isDes=!isDes">
							<text>详细信息</text>
							<text class="iconfont icon-more more" :style="isDes?'transform: rotate(-90deg);':'transform: rotate(90deg);'"></text>
						</view>
					</view>
				</view>
				<view class="use-status" v-if="TabShow != 1">
					<text v-if="TabShow === 2">已使用</text>
					<text v-else-if="TabShow === 3">已过期</text>
				</view>
				<!-- <view class="describe" v-show="isDes">
					<text>限品类：仅可购买会员专区商品</text>
					<text></text>
				</view> -->
			</view>
		</view>
		<view class="empty-box" v-if="goodsList.length==0">
			<image src="/static/ic_nodata.png" mode=""></image>
			<view class="">暂无优惠券</view>
		</view>
		<DialogBox ref="DialogBox"></DialogBox>
	</view>
</template>

<script>
export default {
	data() {
		return {
			TabShow: 1,
			isDes: false,
			goodsList:[],
			page:1
		};
	},
	onShow() {
		let that = this
		this.page=1
		this.goodsList=[]
		this.getList()
	},
	onReachBottom(){
		this.page++
		this.getList()
	},
	methods:{
		/**
		 * 优惠券tab点击
		 * @param {Number} type
		 */
		onCouponTab(type){
			
			this.TabShow = type;
			this.page=1
			this.goodsList = []
			this.getList()
		},
		getList(){
			let that = this
			that.tui.request("api.auth.order/couponList", "POST", {page:that.page,status:that.TabShow}, false, false, false).then((res) => {
				if(res.code==1){
					that.goodsList = that.goodsList.concat(res.data.list)
					
				}
			}).catch((res) => {
				
			})
		},
		/**
		 * 去使用点击
		 */
		onCouponUse(e){
			let that = this
			if(e.type==3&&e.goods_coupon_code==0){
				this.$refs['DialogBox'].confirm({
					title: '提示',
					content: '是否要使用优惠券兑换'+e.name+'?',
					DialogType: 'inquiry',
					animation: 0
				}).then(() => {
					that.tui.request("api.auth.order/exchangeShop", "POST", {coupon_id:e.id}, false, false, true).then((res) => {
						that.tui.toast(res.info)
						if(res.code==1){
							that.page=1
							that.goodsList = []
							that.getList()
						}
					}).catch((res) => {
						
					})
				})
			}else if(e.type==3&&e.goods_coupon_code!=0){
				that.tui.href("/pages/GoodsDetails/GoodsDetails?id="+e.goods_coupon_code)
			}else{
				uni.navigateTo({
					url: '/pages/SearchGoodsList/SearchGoodsList?id=10'
				})
			}
			
		}
	}
};
</script>

<style scoped lang="scss">
@import 'MyCoupon.scss';
</style>
